package cn.lijida.weiwei.favor.modules.sys.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.lijida.weiwei.favor.modules.sys.entity.FavorToken;
import cn.lijida.weiwei.favor.modules.sys.mapper.FavorTokenMapper;
import cn.lijida.weiwei.favor.modules.sys.service.IFavorTokenService;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-12-29
 */
@Service
@Slf4j
public class FavorTokenServiceImpl extends ServiceImpl<FavorTokenMapper, FavorToken> implements IFavorTokenService {

	@Override
	public Long getMemberId(String token, String deviceId) {
		
		QueryWrapper<FavorToken> queryWrapper = new QueryWrapper<>();
		queryWrapper
			.eq("device_id", deviceId)
			.eq("token", token)
			.ge("expiration_date", new Date())
			.last("LIMIT 1");
		FavorToken tokenEntity = this.getOne(queryWrapper);
		if(tokenEntity != null){
			return tokenEntity.getMemberId();
		}
		return null;
	}

	@Override
	public void logout(String token, String deviceId) {
		QueryWrapper<FavorToken> queryWrapper = new QueryWrapper<>();
		queryWrapper
			.eq("member_id", deviceId)
			.eq("token", token)
			.last("LIMIT 1");
		boolean status = this.remove(queryWrapper);
		if(status){
			log.info("退出登录成功。token:"+token+",deviceId:"+deviceId);
		}else{
			log.warn("退出登录失败。token:"+token+",deviceId:"+deviceId);
		}
	}

}
